/**
 * Project Name:BGIDXWS
 * File Name:MyLoaderClass.java
 * Package Name:com.bgi.dx.pub.utils
 * Date:2014-11-18下午01:58:51
 * Copyright (c) 2014, BGI_DX All Rights Reserved.
 *
*/

package com.bgi.uims.pub.utils;

import java.lang.reflect.Method;

/**
 * ClassName:MyLoaderClass <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2014-11-18 下午01:58:51 <br/>
 * @author   fukun
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
public class MyLoaderClass {
	
	public static void main(String[] args) {
		//LoadClass("com.bgi.dx.pub.utils.TestData","PrintInfo","我的","XML");
		LoadClassEx("com.bgi.dx.pub.utils.TestData","PrintInfo","我的","JSON");
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String LoadClass(String clsName,String method,String xmlString,String returnType) {
		String result=null;
		try {
			Class c = Class.forName(clsName);
			Object o = c.newInstance();
			Class[] parameterTypes = new Class[2];//调用的方法有两个参数
			parameterTypes[0] = String.class;//这个参数的类型是String型的
			parameterTypes[1] = String.class;//这个参数的类型也是String型的
			Method m = c.getMethod(method, parameterTypes);
			result=(String) m.invoke(o,xmlString,returnType);//执行
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String LoadClassEx(String clsName,String method,String xmlString,String returnType) {
		String result=null;
		try {
			Thread t = Thread.currentThread();
			ClassLoader cl = t.getContextClassLoader();
			Class c = cl.loadClass(clsName);
			Object o = c.newInstance();
			Class[] parameterTypes = new Class[2];//调用的方法有两个参数
			parameterTypes[0] = String.class;//这个参数的类型是String型的
			parameterTypes[1] = String.class;//这个参数的类型也是String型的
			Method m = c.getMethod(method, parameterTypes);
			result=(String) m.invoke(o,xmlString,returnType);//执行
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}

